package mathMethod;

import java.util.Random;

public class ShuffleArray {

    /**
     * LeetCode T384 打乱数组
     * @param args
     */
    public static void main(String[] args) {

    }
}

class Solution {

    private int[] array;
    private int[] original;

    private Random random = new Random();

    public Solution(int[] nums) {
        this.original = nums;
        this.array = nums.clone();
    }

    /** Resets the array to its original configuration and return it. */
    public int[] reset() {
        this.array = original.clone();
        return this.array;
    }
    //获得随机数
    private int getRandom(int min, int max){
        return random.nextInt(max - min) + min;
    }

    //交换两个数字的位置
    private void swap(int[] nums, int i, int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
    /** Returns a random shuffling of the array. */
    public int[] shuffle() {
        for (int i = 0; i < array.length - 1; i++){
            swap(this.array, i, getRandom(i, array.length));
        }
        return this.array;
    }
}
